package com.huilian.iotbox.admin.controller;

import com.huilian.iotbox.admin.service.MessageLogService;
import com.huilian.iotbox.admin.service.UserService;
import com.huilian.iotbox.data.common.ServerResponse;
import com.huilian.iotbox.data.controller.BaseController;
import com.huilian.iotbox.data.dto.MerchantStoreDto;
import com.huilian.iotbox.data.dto.MessageLogDto;
import com.huilian.iotbox.data.enums.RoleEnum;
import com.huilian.iotbox.data.vo.UserInfo;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Calendar;
import java.util.Date;

/**
 * @author LinYinDong
 * @date 2024/7/30 下午3:32
 */

@RestController
@RequestMapping("/api/messageLog")
public class MessageLogController extends BaseController {
    @Resource
    private MessageLogService messageLogService;
    @Resource
    private UserService userService;

    @PostMapping("/findMessageLogList")
    public ServerResponse findMessageLogList(@RequestBody MessageLogDto messageLogDto, HttpServletRequest request) {
        UserInfo userInfo = userService.getLoginUserInfo(request);
        if (!RoleEnum.SUPER_ADMINISTRATOR.getRoleId().equals(userInfo.getRoleId())) {
            return ServerResponse.createByErrorMessage("你没有权限");
        }
        boolean flag = compareReqTimeSectionMonth(messageLogDto.getCreateTime(),messageLogDto.getUpdateTime());
        if(!flag){
            return ServerResponse.createByErrorMessage("请求异常，时间间隔在一天内");
        }
        messageLogDto.setUserInfo(userInfo);
        messageLogDto.setPageNum(Integer.parseInt(request.getParameter("pageNum")));
        messageLogDto.setPageSize(Integer.parseInt(request.getParameter("pageSize")));
        return messageLogService.findMessageLogList(messageLogDto);
    }

    private boolean compareReqTimeSectionMonth(Date beginTime, Date endTime) {
        Calendar calendar = Calendar.getInstance(); // 得到日历
        if (beginTime == null) {
            return false;
        }
        calendar.setTime(beginTime); // 把当前时间赋给日历
        calendar.add(Calendar.DATE, 1); // 添加一个月
        beginTime = calendar.getTime();
        int i = beginTime.compareTo(endTime);
        return i >= 0;
    }

}
